/**
 * @ClassName KP32_BinaryTreeLevelOrderTraversalII
 * @Description: TODO
 * @Author: JCC
 * @CreateTime: 2023/10/27 12:16
 */

import com.sun.rowset.internal.Row;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.locks.ReadWriteLock;

/**
 * 二叉树的层序遍历
 */
public class KP32_BinaryTreeLevelOrderTraversalII {
    // 测试链接：https://leetcode.com/problems/binary-tree-level-order-traversal-ii


    public static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        TreeNode(int val) {
            this.val = val;
        }
    }

    /**
     * 层序遍历，翻转数组
     *
     * @param root
     * @return
     */
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> ans = new LinkedList<>();
        if (root == null) {
            return ans;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int len = queue.size();
        while (!queue.isEmpty()) {
            List<Integer> row = new LinkedList<>();
            while (len > 0) {
                TreeNode node = queue.poll();
                row.add(node.val);
                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }
                len--;
            }
            ans.add(row);
            len = queue.size();
        }
        int size = ans.size();
        for (int i = 0; i < size / 2; i++) {
            List<Integer> tmp = ans.get(i);
            ans.set(i, ans.get(size - i - 1));
            ans.set(size - i - 1, tmp);
        }
        return ans;
    }

    /**
     * 用链表头插的方法
     * @param root
     * @return
     */
    public List<List<Integer>> levelOrderBottom2(TreeNode root) {
        List<List<Integer>> ans = new LinkedList<>();
        if (root == null) {
            return ans;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int len = queue.size();
        while (!queue.isEmpty()) {
            List<Integer> row = new LinkedList<>();
            while (len > 0) {
                TreeNode node = queue.poll();
                row.add(node.val);
                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }
                len--;
            }
            ans.add(0,row);
            len = queue.size();
        }
        return ans;
    }

    /**
     * 测试ArrayList和LinkedList头插的时间复杂度
     */
    public static void test() {
        int testTimes = 100000;
        ArrayList<Integer> arrayList = new ArrayList<>();
        System.out.println("testArrayList");
        long start = System.currentTimeMillis();
        for (int i = 0; i < testTimes; i++) {
            arrayList.add(0,i);
        }
        long end = System.currentTimeMillis();
        System.out.println(end-start);

        System.out.println("testArrayList");
        start = System.currentTimeMillis();
        List<Integer> linkedList = new LinkedList<>();
        for (int i = 0; i < testTimes; i++) {
            linkedList.add(0,i);
        }
        end = System.currentTimeMillis();
        System.out.println(end-start);
    }

    public static void main(String[] args) {
        test();
    }
}
